package com.example.morealipy.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;

import com.example.morealipy.R;
import com.example.morealipy.callback.ItemDragHelperCallback;
import com.example.morealipy.model.AllApplyTable;

import java.util.Collections;
import java.util.List;

/**
 * <pre>
 *     author : wangkui
 *     e-mail : wangkui20090909@sina.com
 *     time   : 2019/10/14 16:59
 *     desc   :
 *     version: 1.0
 * </pre>
 */
public class ApplyAllAdapter  extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements ItemDragHelperCallback.OnItemMoveListener {
    private final Context context;
    private final LayoutInflater inflater;
    private List<AllApplyTable> mTables;
    private boolean edit = false;//编辑
    private ItemDragHelperCallback mItemDragHelperCallback;
    private ApplyAdapter.OnItemClickListenerEdit mOnItemClickListenerEeit;
    private ApplyAdapter.OnItemClickListener mOnItemClickListener;
    private  ApplyAdapter allApplyAdapter;

    public ApplyAllAdapter(Context context, List<AllApplyTable> tables) {
        this.context = context;
        this.mTables = tables;
        this.inflater = LayoutInflater.from(context);

    }

    /**
     * 设置编辑状态
     *
     * @param edit
     */
    public void setEdit(boolean edit) {
        this.edit = edit;
        this.notifyDataSetChanged();
    }

    /**
     * 获取当前的编辑状态
     *
     * @return 当前的编辑状态
     */
    public boolean getEdit() {
        return edit;
    }

    public void setItemDragHelperCallback(ItemDragHelperCallback itemDragHelperCallback) {
        mItemDragHelperCallback = itemDragHelperCallback;
    }

    /**
     * 设置状态编辑的点击事件
     *
     * @param onItemClickListener
     */
    public void setOnItemClickListenerEdit(ApplyAdapter.OnItemClickListenerEdit onItemClickListener) {
        mOnItemClickListenerEeit = onItemClickListener;
    }

    /**
     * 设置状态编辑的点击事件
     *
     * @param onItemClickListener
     */
    public void setOnItemClickListener(ApplyAdapter.OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_apply_title, parent, false);
        ApplyAllAdapter.ChannelViewHolder mChannelViewHolder = new ApplyAllAdapter.ChannelViewHolder(view);
        return  mChannelViewHolder;
    }

    @Override
    public int getItemViewType(int position) {
        return mTables.get(position).getType();
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        AllApplyTable table = mTables.get(position);
        ApplyAllAdapter.ChannelViewHolder mholder = (ApplyAllAdapter.ChannelViewHolder) holder;
        mholder.mRv_all_item.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false));
        allApplyAdapter = new ApplyAdapter(context,  table.getmList() );
        mholder.mRv_all_item.setAdapter(allApplyAdapter);
//        mholder.mTextView.setText(table.getName());
//        mholder.mImageView.setImageResource(table.getImgRes());
//        handleOnClick(mholder, table);//设置点击监听
//        handleLongPress(mholder, table);//设置长按监听
    }

    //处理点击事件
//    private void handleOnClick(final ApplyAllAdapter.ChannelViewHolder holder, final ApplyTable table) {
//        holder.iv_add.setVisibility(View.VISIBLE);
//        if (mOnItemClickListenerEeit != null) {
//            if (table.getState() == 0) {//如果在头部，就取消点击事件
//                holder.iv_add.setOnClickListener(null);
//                holder.iv_add.setImageResource(R.mipmap.item_added);
//            } else if (table.getState() == 1) {
//                holder.iv_add.setImageResource(R.mipmap.item_add);
//                holder.iv_add.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        //对项目点击后增删操作的监听
//                        mOnItemClickListenerEeit.onItemClick(view, holder.getLayoutPosition());
//                        tarnslationAnimation(holder.mLayout);
//                    }
//                });
//            }
//        }
//        if (edit) {
//            holder.iv_add.setVisibility(View.GONE);
//            holder.mImageView.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                    mOnItemClickListener.onItemClick(view, table.getName());
//                }
//            });
//        } else {
//            holder.mImageView.setOnClickListener(null);
//        }
//    }


    private void tarnslationAnimation(View view){
        final float x = view.getX();
        final float y = view.getY();
        final float targetx = 200;
        final float targety = 400;
        view.animate().translationX(-(x-targetx))
                .translationY(-(y-targety))
                .setDuration(3000)
                .setInterpolator(new AccelerateInterpolator())
                .withLayer()
                .start();


    }
    //处理长按事件 开启ItemDragHelperCallBack拖拽
//    private void handleLongPress(ApplyAllAdapter.ChannelViewHolder holder, final ApplyTable table) {
//        if (mItemDragHelperCallback != null) {
//            holder.mLayout.setOnTouchListener(new View.OnTouchListener() {
//                @Override
//                public boolean onTouch(View v, MotionEvent event) {
//                    mItemDragHelperCallback.setLongPressEnabled(table.getIndex() == 0 ? false : true);
//                    return false;
//                }
//            });
//        }
//    }

    @Override
    public int getItemCount() {
        return mTables == null ? 0 : mTables.size();
    }

    @Override
    public boolean onItemMove(int fromPosition, int toPosition) {
        if (isChannelFixed(fromPosition, toPosition)) {
            return false;
        }
        //在我的频道子频道的移动
        Collections.swap(getAdapterData(), fromPosition, toPosition);
        notifyItemMoved(fromPosition, toPosition);
        //通知顺序变换，存储，设置频道顺序，以及显示的顺序
        System.out.println("发送移动的消息");
//        EventBus.getDefault().post(new ChannelBean(getAdapterData()));
        return true;
    }

    //不能移动头条
    private boolean isChannelFixed(int fromPosition, int toPosition) {
        return fromPosition == 0 || toPosition == 0;
    }

    public class ChannelViewHolder extends RecyclerView.ViewHolder {
//        private TextView mTextView;//文字描述
//        private ImageView mImageView, iv_add;//图片,添加删除按钮
//        private FrameLayout mLayout;//整个item布局
        private final RecyclerView mRv_all_item;

        public ChannelViewHolder(View itemView) {
            super(itemView);
            mRv_all_item = itemView.findViewById(R.id.mRv_all_item);
//            mImageView = (ImageView) itemView.findViewById(R.id.iv_channel_logo);
//            iv_add = (ImageView) itemView.findViewById(R.id.iv_add);
//            mTextView = (TextView) itemView.findViewById(R.id.tv_channel_name);
//            mLayout = (FrameLayout) itemView.findViewById(R.id.rl_root);
        }
    }
//    public class ChannelTitleViewHolder extends RecyclerView.ViewHolder {
//        private TextView tv_channel_title_name;//文字描述
//
//        public ChannelTitleViewHolder(View itemView) {
//            super(itemView);
//            tv_channel_title_name = (TextView) itemView.findViewById(R.id.tv_channel_title_name);
//        }
//    }

    public List<AllApplyTable> getAdapterData() {
        return mTables;
    }

    //状态编辑Item点击事件的监听接口
    public interface OnItemClickListenerEdit {
        void onItemClick(View view, int position);
    }

    //Item点击事件的监听接口
    public interface OnItemClickListener {
        void onItemClick(View view, String name);
    }
}
